package com.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

public class Http {
	private static Logger logger = Logger.getLogger(Http.class);  
	  
    public static String doPost(String url, Map<String, String> params, String charset) {  
        StringBuffer response = new StringBuffer();  
        HttpClient client = new HttpClient();  
        PostMethod method = new PostMethod(url);  
        // 设置Http Post数据  
        method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=" + charset);  
        if(params != null){  
            Set<String> keySet = params.keySet();  
            NameValuePair[] param = new NameValuePair[keySet.size()];  
            int i = 0;  
            for(String key : keySet){  
                param[i] = new NameValuePair(key, params.get(key));  
                i++;  
            }  
            method.setRequestBody(param);  
        }  
        InputStream responseBodyStream = null;  
        InputStreamReader streamReader = null;  
        BufferedReader reader = null;  
        try {  
            client.executeMethod(method);  
            if (method.getStatusCode() == HttpStatus.SC_OK) {  
                responseBodyStream = method.getResponseBodyAsStream();  
                streamReader = new InputStreamReader(responseBodyStream, charset);  
                reader = new BufferedReader(streamReader);  
                String line;  
                while ((line = reader.readLine()) != null) {  
                    response.append(line);  
                }  
            }  
        } catch (IOException e) {  
            logger.error("执行HTTP Post请求" + url + "时，发生异常！", e);  
        } finally {  
            try {  
                responseBodyStream.close();  
                streamReader.close();  
                reader.close();  
            } catch (IOException e) {  
                logger.error("执行HTTP Post请求" + url + "时，发生异常，关闭流异常！", e);  
                e.printStackTrace();  
            }  
            method.releaseConnection();  
        }  
        return response.toString();  
}
    public static void main(String[] args) {  
        Map<String, String> mapParam = new HashMap<String, String>();  
        	mapParam.put("mobile", "13424367370");  
	        mapParam.put("title","e安全");  
	        mapParam.put("content","李江杰刷脸回家");  
	        mapParam.put("soucre","1");  
	        mapParam.put("sign","d60d4f003a7df267f235445b8abf2c49");  
	        mapParam.put("ts","1517193355");
	        mapParam.put("appID","ICEEANQUAN-ADC5-42B8-BB2E-BC39B211FDED");
	        String result= Http.doPost("https://openapi.colourlife.com/v1/czyprovide/pushInformation", mapParam, "utf-8");  
	        System.out.println(result);
    }  
}
